/*
 * Copyright (c) 2006, 2022, webrx.cn All rights reserved.
 *
 */
package cn.webrx;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * <p>Project: app2022 - D2
 * <p>Powered by webrx On 2022-01-05 15:41:17
 *
* @author webrx [webrx@126.com]
 * @version 1.0
 * @since 17
 */
public class D3 {
    public static void main(String[] args) throws ParseException {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        var d = new Date();
        System.out.println(sdf.format(d));
        System.out.printf("%tF %<tT %n", d);
        System.out.println(String.format("%tF %<tT %n", d));

        String regtime = "2000-12-05 08:22:31";

        var d2 = sdf.parse(regtime);  //java.util.Date 类的实例（对象）
        long time = d2.getTime();

        //d2.getDay() 返回 0 - 6 0 星期日

        var d3 = new Date(time);

        var c = Calendar.getInstance();
        c.add(Calendar.DATE, 3);
        int wk = c.get(Calendar.DAY_OF_WEEK); //1 - 7 1星期日 2星期一 5 星期四

        if (wk == 1) System.out.println("今日：星期日");
        if (wk == 2) System.out.println("今日：星期一");
        if (wk == 3) System.out.println("今日：星期二");
        if (wk == 4) System.out.println("今日：星期三");
        if (wk == 5) System.out.println("今日：星期四");
        if (wk == 6) System.out.println("今日：星期五");
        if (wk == 7) System.out.println("今日：星期六");

        System.out.println("今日：星期" + "日一二三四五六".charAt(wk - 1));


        //1.计算 两个日期之间的差值 天
        var dd1 = sdf.parse("2022-01-01 00:00:00");
        var dd2 = new Date();
        long day = (dd2.getTime() - dd1.getTime()) / (1000 * 60 * 60 * 24);
        System.out.println(day);

        //2.返回 当前日期，9天前的日期
        long time3 = dd2.getTime() - (1000 * 60 * 60 * 24 * 9);
        System.out.printf("%tF %<tT%n", time3);

        var c3 = Calendar.getInstance();
        c3.add(Calendar.DATE, -9);
        System.out.printf("%tF %<tT%n", c3);

        //7天内注册会员数，7天内交易总额
        //select count(*) from meber regtime start and end

        //3.返回当前第一天，最后一天
        var c4 = Calendar.getInstance();

        //目前时间
        System.out.printf("%tF %<tT%n", c4);

        //第一天时间
        c4.set(Calendar.DAY_OF_MONTH, 1);
        c4.set(Calendar.HOUR, 0);
        c4.set(Calendar.MINUTE, 0);
        c4.set(Calendar.SECOND, 0);
        System.out.printf("%tF %<tT%n", c4);

        //本月最后一天的日期
        c4.add(Calendar.MONTH, 1);
        c4.set(Calendar.DAY_OF_MONTH, 1);
        c4.add(Calendar.DATE, -1);
        c4.set(Calendar.HOUR, 23);
        c4.set(Calendar.MINUTE, 59);
        c4.set(Calendar.SECOND, 59);
        System.out.printf("%tF %<tT%n", c4);

        //4. 计算某年某月那天是星期几
        var sdf3 = new SimpleDateFormat("yyyy-MM-dd");
        var ddd3 = sdf3.parse("2022-01-06");
        System.out.printf("%tA%n", ddd3);

    }
}
